package util.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import java.awt.Dimension;

import util.Debug.ErrorReport;
import util.ui.TCWEditorPane;

public class UserPrompt {
	public static final Color PROMPT = new Color(0xEEFFEE);
	
	public static void displayInfo(JFrame parentFrame, String title, String [] message, boolean isModal) {
		JOptionPane pane = new JOptionPane();
		pane.setMessage(message);
		pane.setMessageType(JOptionPane.PLAIN_MESSAGE);
		JDialog helpDiag = pane.createDialog(parentFrame, title);
		helpDiag.setModal(isModal);
		helpDiag.setVisible(true);		
	}
	
	public static void displayInfo(String title, String [] message, boolean isModal) {
		displayInfo(null, title, message, isModal);
	}
	
	public static void displayInfo(String title, String [] message) {
		displayInfo(null, title, message, false);
	}
	public static void displayInfoMonoSpace(Component parentFrame, String title, String [] message, boolean isModal, boolean sizeToParent) {
		JOptionPane pane = new JOptionPane();
		
		String theMessage = "";
		for(int x=0; x<message.length; x++)
			theMessage += message[x] + "\n";
		JTextArea messageArea = new JTextArea(theMessage);

		JScrollPane sPane = new JScrollPane(messageArea); 
		messageArea.setFont(new Font("monospaced", Font.BOLD, 12));
		messageArea.setEditable(false);
		messageArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		pane.setMessage(sPane);
		pane.setMessageType(JOptionPane.PLAIN_MESSAGE);

		JDialog helpDiag = pane.createDialog(parentFrame, title);
		helpDiag.setModal(isModal);
		helpDiag.setResizable(true);
		
		if(sizeToParent && parentFrame != null && (helpDiag.getWidth() >= parentFrame.getWidth() || helpDiag.getHeight() >= parentFrame.getHeight()))
				helpDiag.setSize(parentFrame.getSize());
		helpDiag.setVisible(true);		
	}
	public static void displayInfoTable(JFrame parentFrame, String title, final String [] message, boolean isModal, boolean sizeToParent) {
		final class TheModel extends AbstractTableModel {
			private static final long serialVersionUID = 2153498168030234218L;

			public int getColumnCount() {
				if(message == null || message.length == 0)
					return 1;
				return message[0].split("\\s").length; 
			}
			public int getRowCount() { return message.length; }
			public Object getValueAt(int row, int col) {
				if(row < message.length && col < message[row].split("\\s").length)
					return message[row].split("\\s")[col];
				return "";
			}
		}

		JOptionPane pane = new JOptionPane();
		final JButton btnCopySeqID = new JButton("Copy Seq ID");
	
		final JTable messageTable = new JTable();
		messageTable.setModel(new TheModel());
		messageTable.setFont(new Font("monospaced", Font.BOLD, 12));
		messageTable.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				btnCopySeqID.setEnabled(messageTable.getSelectedRow() >= 0);
			}
		});

		btnCopySeqID.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnCopySeqID.setEnabled(false);
		btnCopySeqID.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = messageTable.getSelectedRow();
				
				String rowVal = (String)messageTable.getValueAt(row, 1);
				int strPos = 0;
				if((strPos = rowVal.indexOf('|')) >= 0)
					rowVal = rowVal.substring(strPos+1);
				
				Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
				cb.setContents(new StringSelection(rowVal), null);
			}
		});
		
		JScrollPane sPane = new JScrollPane(messageTable); 
		messageTable.setTableHeader(null);
		messageTable.setShowGrid(false);
		messageTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		messageTable.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		sPane.getViewport().setBackground(Color.WHITE);
		
		//Adjust column sizes
        TableColumn column;
        Component comp;
        int cellWidth;
        
        for (int i = 0;  i < messageTable.getColumnCount();  i++) { // for each column
            column = messageTable.getColumnModel().getColumn(i);
            
            cellWidth = 0;
            for (int j = 0;  j < messageTable.getModel().getRowCount();  j++) { // for each row
	            comp = messageTable.getDefaultRenderer(messageTable.getColumnClass(i)).
	                             getTableCellRendererComponent(
	                            		 messageTable, messageTable.getValueAt(j, i),
	                                 false, false, j, i);

	            if(comp != null) {
		            cellWidth = Math.max(cellWidth, comp.getMinimumSize().width);
	            }
            }

            column.setPreferredWidth(cellWidth);
        }
        
		JPanel buttonRow = new JPanel();
		buttonRow.setLayout(new BoxLayout(buttonRow, BoxLayout.LINE_AXIS));
		
		buttonRow.add(btnCopySeqID);
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		
		mainPanel.add(buttonRow);
		mainPanel.add(Box.createVerticalStrut(15));
		mainPanel.add(sPane);
		
		pane.setMessage(mainPanel);
		pane.setMessageType(JOptionPane.PLAIN_MESSAGE);

		JDialog helpDiag = pane.createDialog(parentFrame, title);
		helpDiag.setModal(isModal);
		helpDiag.setResizable(true);
				
		if(sizeToParent && parentFrame != null && (helpDiag.getWidth() >= parentFrame.getWidth() || helpDiag.getHeight() >= parentFrame.getHeight()))
				helpDiag.setSize(parentFrame.getSize());
		helpDiag.setVisible(true);	
		
	}
	
	public static void displayHTMLResourceHelp(Component parentFrame, String title, String urlstr) {
		try {
			if (!urlstr.startsWith("/")) urlstr = "/" + urlstr;
			TCWEditorPane hlpPane = new TCWEditorPane(UserPrompt.class.getResource(urlstr));
	        
			hlpPane.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
			hlpPane.setMaximumSize(hlpPane.getPreferredSize());
			hlpPane.setMinimumSize(hlpPane.getPreferredSize());
			
			JScrollPane sPane = new JScrollPane(hlpPane);
			sPane.setMinimumSize(hlpPane.getPreferredSize());
			JDialog hlpDiag = new JDialog();
			hlpDiag.setBackground(Color.WHITE);
			hlpDiag.setModal(false);
			hlpDiag.add(sPane, BorderLayout.CENTER);
			hlpDiag.pack();
			// WN: try to size to content (was opening to full screen)
			Dimension d = hlpPane.getPreferredSize();
			Dimension dmax = parentFrame.getPreferredSize();
			d.width += 100;
			d.height += 100;
			if (d.height > dmax.height) d.height = dmax.height;
			hlpDiag.setSize(d);

			hlpDiag.setTitle(title);
			hlpDiag.setVisible(true);
		}
		catch(Exception ex) {
			ErrorReport.prtReport(ex, "Error loading manager help");
		}
	}
/*	public static class TCWEditorPane extends javax.swing.JEditorPane 
	{
		public TCWEditorPane(URL linkURL) throws Exception 
		{
			super(linkURL);
			
	        Font font = UIManager.getFont("Label.font");
	        String bodyRule = "body { font-family: " + font.getFamily() + "; " +
	                "font-size: " + font.getSize() + "pt; }";
	        ((HTMLDocument)getDocument()).getStyleSheet().addRule(bodyRule);

			//HTMLEditorKit editorKit = 
			//setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
			
			addHyperlinkListener(new HyperlinkListener() 
			{
			    public void hyperlinkUpdate(HyperlinkEvent e) 
			    {
			        if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
			        {
			        	
		        		try
		        		{
		        			String ustr = e.getDescription();
				        	//System.err.println("link:" + ustr);
		        			if (ustr.startsWith("http://"))
		        			{
					        	if(Desktop.isDesktopSupported()) 
					        	{
					        		Desktop.getDesktop().browse(e.getURL().toURI());
					        	}		        				
		        			}
		        			else
		        			{
		        				URL url = TCWEditorPane.class.getResource(ustr);
		        				setPage(url);
		        			}

		        		}
		        		catch(Exception eee)
		        		{
		        			System.err.println("Can't open link");
		        			System.err.println(eee.getMessage());
		        		}
			        }
			    }
			});
		}

		protected void paintComponent(Graphics g) 
		{
			Graphics2D g2 = (Graphics2D)g;

			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

			super.paintComponent(g2);
		}
	}
	public static void displayHTMLResourceHelp2(Component parentFrame, String title, String url) {
		try {
			if (!url.startsWith("/")) url = "/" + url;
			URL helpURL = UserPrompt.class.getResource(url);
			JEditorPane hlpPane = new JEditorPane(helpURL);
			hlpPane.setContentType("text/html");
			hlpPane.setEditable(false);
			hlpPane.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
			hlpPane.setMaximumSize(hlpPane.getPreferredSize());
			hlpPane.setMinimumSize(hlpPane.getPreferredSize());
			
			JScrollPane sPane = new JScrollPane(hlpPane);
			sPane.setMinimumSize(hlpPane.getPreferredSize());
			JDialog hlpDiag = new JDialog();
			hlpDiag.setBackground(Color.WHITE);
			hlpDiag.setModal(true);
			hlpDiag.add(sPane, BorderLayout.CENTER);
			hlpDiag.pack();
			// WN: try to size to content (was opening to full screen)
			Dimension d = hlpPane.getPreferredSize();
			Dimension dmax = parentFrame.getPreferredSize();
			d.width += 100;
			d.height += 100;
			if (d.height > dmax.height) d.height = dmax.height;
			hlpDiag.setSize(d);

			hlpDiag.setTitle(title);
			hlpDiag.setVisible(true);
		}
		catch(Exception ex) {
			ErrorReport.prtReport(ex, "Error loading manager help");
		}
	}
	*/
}
